
package com.game;

import java.util.Date;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;


/**
 * 
 * @author Harmonia Game Studio
 *
 */
public class MainThread extends Thread
{
	/**
	 * 
	 */
	private static final String TAG = MainThread.class.getSimpleName();
	
	/**
	 * 
	 */
	private SurfaceHolder surfaceHolder;
	
	/**
	 * 
	 */
	private MainGamePanel gamePanel;

	/**
	 * 
	 */
	private boolean running;
	
	/**
	 * 
	 * @param running
	 */
	public void setRunning(boolean running)
	{		
		this.running = running;
	}

	/**
	 * 
	 * @param surfaceHolder
	 * @param gamePanel
	 */
	public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel)
	{
		//
		super();
		
		//
		this.surfaceHolder = surfaceHolder;
		
		//
		this.gamePanel = gamePanel;
	}

	/**
	 * 
	 */
	@Override
	public void run()
	{
		//
		Canvas canvas;
		
		//
		Log.d(TAG, "run game loop");
		
		//
		while (running)
		{
			//
			canvas = null;
			
			//
			try 
			{
				//
				canvas = this.surfaceHolder.lockCanvas();
				
				//
				synchronized (surfaceHolder)
				{
					//
					this.gamePanel.onUpdate(System.currentTimeMillis());
					this.gamePanel.onDraw(canvas);				
				}
			}
			//
			finally
			{
				//
				if (canvas != null)
				{
					//
					surfaceHolder.unlockCanvasAndPost(canvas);
				}
			}
		}
	}
}
